/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladores;

import beans.EjemploBean;
import entidades.Persona;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author jose maria
 */
@WebServlet(name = "EjemploServlet", urlPatterns = {"/EjemploServlet"})
public class EjemploServlet extends HttpServlet {

    @EJB
    private EjemploBean ejemploBean;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void insertarPersona(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet EjemploServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet EjemploServlet at " + request.getContextPath() + "</h1>");
            out.println("<h2>Insertanto una persona nueva...</h2>");
            Persona p = new Persona();
            p.setNombre("abc" + System.currentTimeMillis());
            Long clave = ejemploBean.insertarPersona(p);
            out.println("<h3>Se ha insertado en la base de datos una nueva persona con clave " + clave + "</h3>");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    protected void mostrarPersonas(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet EjemploServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet EjemploServlet at " + request.getContextPath() + "</h1>");
            out.println("<h2>Mostrando las personas existentes en el sistema...</h2>");
            List<Persona> todas = ejemploBean.todas();
            if (todas.isEmpty()) {
                out.println("<h3>No se encuentran personas en el sistema</h3>");
            } else {
                out.println("<ul>");
                for (Persona persona : todas) {
                    out.println("<li>");
                    out.println(persona);
                    out.println("</li>");
                }
                out.println("</ul>");
            }

            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String listado = request.getParameter("listado");
        if (listado == null) {
            insertarPersona(request, response);
        } else {
            mostrarPersonas(request, response);
        }

    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        insertarPersona(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
